--[[
	FONTE: 
		* https://github.com/BlockMen/hud
		* https://github.com/BlockMen/hud/blob/master/init.lua
]]--

lunogauges.huds.player_air = {}
lunogauges.huds.gaugeair = {}

lunogauges.huds.add_air = function(player)
	if player~= nil and player:is_player() then
		local name = player:get_player_name()
		local breath = player:get_breath()
		lunogauges.huds.player_air[name] = breath
		
		--minetest.chat_send_player(name, "[add] breath='"..breath.."'", false)
		
		lunogauges.huds.gaugeair[name] = {}
		if breath<=10 then
			lunogauges.huds.gaugeair[name].fg = player:hud_add({
				hud_elem_type = "statbar",
				position = {x=0.5,y=1},
				size = {x=32, y=32}, --{x=24, y=24},
				text = "hud_bubble_fg.png",
				number = breath * 2,
				alignment = {x=-1,y=-1},
				offset = {x=-115, y=-137},
			})
		else
			lunogauges.huds.gaugeair[name].fg = player:hud_add({
				hud_elem_type = "statbar",
				position = {x=0.5,y=1},
				size = {x=32, y=32}, --{x=24, y=24},
				text = "hud_bubble_fg.png",
				number = 0,
				alignment = {x=-1,y=-1},
				offset = {x=-115, y=-137},
			})
		end
	end
end

lunogauges.huds.update_air= function(player)
	if player~= nil and player:is_player() then
		local name = player:get_player_name()
		local breath = player:get_breath()
		--minetest.chat_send_player(name, "[update] breath='"..breath.."'", false)
		if lunogauges.huds.player_air[name]== nil or lunogauges.huds.player_air[name] ~= breath   then
			lunogauges.huds.player_air[name] = breath
			if lunogauges.huds.gaugeair[name]~=nil and lunogauges.huds.gaugeair[name].fg ~= nil then
				if breath<=10 then
					player:hud_change(lunogauges.huds.gaugeair[name].fg, "number", breath * 2)
				else
					player:hud_change(lunogauges.huds.gaugeair[name].fg, "number", 0) --Fica invisivel se (breath>=11)
				end
			else
				lunogauges.huds.add_air(player)
			end
		end
	end
end

